//=============================================================================
//                                                                            
//   Exercise code for the lecture
//                                                                            
//=============================================================================
//=============================================================================
//
//  CLASS TerrainViewer
//
//=============================================================================


#ifndef PROCEDURAL_VIEWER_HH
#define PROCEDURAL_VIEWER_HH


//== INCLUDES =================================================================
#include <string>
#include <vector>

#include "../../gl/TrackballViewer.h"
#include "../../gl/shader.h"
#include "../../gl/Light3D.h"
#include "../../gl/texture.h"
#include "../../gl/fbo.h"

//== CLASS DEFINITION =========================================================

#define MAP_Z 512
#define MAP_X 512
	      
/*
 TerrainViewer.
*/
class TerrainViewer : public TrackballViewer
{
public:
   
	TerrainViewer(const char* _title, int _width, int _height);
  
	void loadMesh(const std::string& filenameObj);

protected:

	// overloaded GUI function
	virtual void init();
	virtual void keyboard(int key, int x, int y);
	virtual void special(int key, int x, int y);

	virtual void draw_scene(DrawMode _draw_mode);

protected:
	std::vector<float> createColorMapTexture( int mapSize, const Vector3 * discreteColorMap, int outputTextureSize );
    
private:
	void renderFullScreenQuad();
	bool first_draw;
	float terrain_height[MAP_X][MAP_Z][3];
	float terrain_normal[MAP_X][MAP_Z][3];

protected:
	
	// frame buffer object for render2texture
	FrameBufferObject m_fbo;

	// fractal terrain shader
	Shader m_terrainShader;
	
	// height field shader
	Shader m_heightShader;
	
	// Textures
	Texture m_permutationTable;
	Texture m_gradient2DTable;
	Texture m_gradient3DTable;
	Texture m_gradient4DTable;
	
	Texture m_colorMap;
	
	// Terrain textures
	Texture m_lowest;
	Texture m_medium;
	Texture m_high;
	Texture m_highest;
	
	// fractal terrain height field texture
	Texture m_terrainOutputTexture;
    
    //void draw_vertex(float (*terrain_normal)[MAP_Z][3], float (*terrain_height)[MAP_Z][3], int x, int z);
    
    void make_normal(int x, int z);
	
};


//=============================================================================
#endif // PROCEDURAL_VIEWER_HH defined
//=============================================================================
